package com.his.admin.controller;

import com.his.admin.service.HospitalService;
import com.his.admin.service.RoleService;
import com.his.dao.domain.dto.ListDto;
import com.his.dao.domain.entity.Hospital;
import com.his.dao.domain.entity.Role;
import com.his.util.annotation.RequiresLogin;
import com.his.util.annotation.RequiresPermissions;
import com.his.util.result.RestResult;
import com.his.util.result.RestResultBuilder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/role")
@AllArgsConstructor
@Api(value = "角色模块")
public class RoleController {
    private RoleService roleService;

    @RequiresLogin
    @RequiresPermissions
    @RequestMapping("/all")
    @ApiOperation(value = "获取角色列表",notes = "获取角色列表")
    public RestResult all(@RequestHeader("authorization") String  authorization, @RequestBody ListDto listDto) {
        return roleService.all(authorization, listDto);
    }

    @RequiresLogin
    @RequiresPermissions
    @RequestMapping("/add")
    @ApiOperation(value = "增加角色",notes = "增加角色")
    public RestResult add(@RequestBody Role role) {
        if (roleService.save(role)) {
            return new RestResultBuilder().success();
        }
        return new RestResultBuilder().fail();
    }

    @RequiresLogin
    @RequiresPermissions
    @RequestMapping("/update")
    @ApiOperation(value = "更新角色",notes = "更新角色")
    public RestResult update(@RequestBody Role role) {
        if (roleService.updateById(role)) {
            return new RestResultBuilder().success();
        }
        return new RestResultBuilder().fail();
    }

    @RequiresLogin
    @RequiresPermissions
    @RequestMapping("/delete")
    @ApiOperation(value = "删除角色",notes = "删除角色")
    public RestResult delete(@RequestBody List<Integer> ids) {
        if (roleService.removeByIds(ids)) {
            return new RestResultBuilder().success();
        }
        return new RestResultBuilder().fail();
    }
}